package us.icitap.timsportal.ws.util;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJBException;

import al.gov.mrp.egjoba.ws.rmis.HandlerClient;

import us.icitap.timsportal.entity.OrgStructure;

public class RmisOSThread extends Thread {
	
	private String name = "";
	private Integer id = null;
	private List<OrgStructure> list;
	
	public RmisOSThread(String name) throws SecurityException{
		this.name = name;
		setDaemon(true);
	}
	
	public RmisOSThread(Integer id)  throws SecurityException{
		this.id = id;
		setDaemon(true);
	}
	
	@Override
	public void run() throws EJBException{
		List<al.gov.mrp.egjoba.ws.rmis.Organization> rmisList = null;
		try {
			if(this.id!=null){
				rmisList = HandlerClient.getSvSoapWebService().listOrganizationId(this.id);				
			}else{
				rmisList = HandlerClient.getSvSoapWebService().listOrganizationSearchName(this.name);
			}
			if(rmisList==null)return;
			list = new ArrayList<OrgStructure>();
			for(al.gov.mrp.egjoba.ws.rmis.Organization o: rmisList){
				OrgStructure os = new OrgStructure();
				os.setId(new Long(o.getIdOrg()).intValue());
				os.setName(o.getOrgName());
				list.add(os);
			}			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage()+" ----- "+"Exception with web-service");
		}		
	
	}
	
	public List<OrgStructure> getList(){
		return this.list;
	}

}
